<?php

class SiteController extends Controller {

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        $data = Confession::model()->listConfessions();
        $this->render('index', $data);
    }

    public function actionRecall() {
        if (!YII_DEBUG && !Yii::app()->request->isAjaxRequest) {
            throw new CHttpException('403', 'Forbidden access.');
        }
        if (empty($_POST['startpoint'])) {
            throw new CHttpException('404', 'Missing "startpoint" POST parameter.');
        }
        $startpoint = $_POST['startpoint'];
        $startpoint = empty($_POST['startpoint']) ? 0 : (int) $_POST['startpoint'];
        $confessions = Confession::model()->getConfessions($startpoint);
        if (count($confessions) == Constant::NUMBER_NEW_PER_PAGE) {
            $startpoint = $startpoint + Constant::NUMBER_NEW_PER_PAGE;
        }
        $this->renderPartial('index', array(
            'confessions' => $confessions,
            'startpoint' => $startpoint
        ));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest) {
                echo $error['message'];
            } else {
                $this->layout = '//layouts/column1';
                $this->render('error', $error);
            }
        }
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
        // display the login form
        $this->layout = '//layouts/column1';
        // set pagetitle
        $this->pageTitle = Yii::t('default', 'Login');

        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
            //$this->redirect(Yii::app()->user->returnUrl);
                $this->redirect(Yii::app()->request->baseUrl . '/admin');
        }

        $this->render('login', array('model' => $model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

}